package com.interview.javabase.thread;

import java.util.concurrent.atomic.AtomicInteger;

/**
 * @description:
 * @author: zhangshuguang@vichain.com
 * @date: 2019/06/14 13:49
 */
public class Demo6 {
    public static void main(String[] args) throws Exception {
        Test2 test2 = new Test2();
        for (int i = 1; i <= 10; i++) {
            new Thread(() -> {
                for (int j = 1; j <= 1000; j++) {
                    test2.changeNumber();
                    test2.getAtomicInteger();
                }
            }, "AAA").start();
        }
        while (Thread.activeCount() > 1) {
            // 让出主线程 等待上面的所有线程都完成后
            Thread.yield();
        }
        System.out.println("总计：" + test2.getNumber());
        System.out.println("总计：" + test2.getAtomicInteger());
    }

}

class Test2 {
    private volatile int number = 0;
    private AtomicInteger atomicInteger = new AtomicInteger();

    public void changeNumber() {
        this.number++;
        atomicInteger.getAndIncrement();
    }

    public AtomicInteger getAtomicInteger() {
        return atomicInteger;
    }

    public Test2 setAtomicInteger(AtomicInteger atomicInteger) {
        this.atomicInteger = atomicInteger;
        return this;
    }

    public int getNumber() {
        return number;
    }

    public Test2 setNumber(int number) {
        this.number = number;
        return this;
    }
}